const {
  resolve
} = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const {
  CleanWebpackPlugin
} = require('clean-webpack-plugin')
const MinaWebpackPlugin = require('./plugin/MinaWebpackPlugin');
const MinaRuntimePlugin = require('./plugin/MinaRuntimePlugin');
const LodashWebpackPlugin = require('lodash-webpack-plugin');
const webpack = require('webpack');
const debuggable = process.env.BUILD_TYPE !== 'release'
module.exports = {
  context: resolve('src'),
  entry: './app.js',
  output: {
    path: resolve('dist'),
    filename: '[name].js',
    globalObject: 'wx'
  },
  plugins: [
    new MinaWebpackPlugin({
      scriptExtensions: ['.js','.ts'],
      assetExtensions: ['.scss'],
    }),
    new MinaRuntimePlugin(),
    new LodashWebpackPlugin(),
    new CleanWebpackPlugin({

      cleanStaleWebpackAssets: false,

    }),
    new CopyWebpackPlugin({
      patterns: [{
        from: resolve('src'),
        to: resolve('dist'),
        globOptions: {
          ignore: ['**/*.ts', '**/*.scss'],
        },
      },
      {
        from:resolve('src/vant-weapp'),
        to:resolve('dist/vant-weapp'),
      }
    ],

    }),
    new webpack.EnvironmentPlugin({
      NODE_ENV: JSON.stringify(process.env.NODE_ENV) || 'development',
      BUILD_TYPE: JSON.stringify(process.env.BUILD_TYPE) || 'debug',
    }),
  
  ],
  module: {
    
    rules: [{
        test: /\.ts$/,
        use: ['babel-loader','ts-loader'],
        exclude:[
          resolve(__dirname,'node_modules/')
        ],
      },
      {
        test: /\.js$/,
        use: ['babel-loader'],
        exclude:[
          resolve(__dirname,'node_modules/'),
          resolve(__dirname,'src/vant-weapp/')
        ],
      },
      {
        test: /\.(scss)$/,
        include: /src/,
        use: [{
            loader: 'file-loader',
            options: {
              useRelativePath: true,
              name: '[path][name].wxss',
              context: resolve('src'),
            },
          },
          {
            loader: 'sass-loader',
            options: {
              sassOptions: {
                indentWidth: 4,
                includePaths: [resolve('src', 'styles'), resolve('src')],
              },
            },
          },
        ],
      },
      {
        test: /\.(png|jpg|gif)$/i,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 10 * 1024,
          },
        }, ],
      },
    ]
  },
  resolve:{
    extensions: ['.ts', '.js', '.json'],
    alias:{
      '@':resolve(__dirname,'./src'),
    }
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
      name: 'common',
      minChunks: 2,
      minSize: 0,
    },
    runtimeChunk: {
      name: 'runtime'
    }
  },
  mode: process.env.NODE_ENV,
  devtool: debuggable ? 'inline-source-map' : 'none',
}